package cn.piggy.gateway.core.executor;

import cn.piggy.gateway.core.datasource.connection.Connection;
import cn.piggy.gateway.core.executor.result.SessionResult;
import cn.piggy.gateway.core.http.HttpStatement;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;


public class SimpleExecutor implements Executor {
    private final Logger logger = LoggerFactory.getLogger(SimpleExecutor.class);

    protected Connection connection;

    public SimpleExecutor(Connection connection) {
        this.connection = connection;
    }

    @Override
    public SessionResult exec(HttpStatement httpStatement, Map<String, Object> params) {
        String parameterType = httpStatement.getParameterType();
        logger.info("执行调用 method：{}#{}.{}({}) args：{}", httpStatement.getApplication(), httpStatement.getInterfaceName(), httpStatement.getMethodName(), parameterType, JSON.toJSONString(params));
        // 抽象方法
        try {
            Object data = connection.execute(httpStatement, params);
            return SessionResult.buildSuccess(data);
        } catch (Exception e) {
            e.printStackTrace();
            return SessionResult.buildError(e.getMessage());
        }
    }

}
